package org.shreker.shan.website.business.interceptor;

import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.shreker.shan.core.domain.Login;
import org.shreker.shan.core.util.LoginContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginInterceptor extends HandlerInterceptorAdapter  {
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HandlerMethod handlerMethod = (HandlerMethod) handler;
		Method method = handlerMethod.getMethod();
		String requestName = method.getName();
		System.out.println("请求:"+method.getName());
		if ("login".equals(requestName) || "register".equals(requestName) ) {
			return true;
		}
		Login login = LoginContext.getCurrentLogin();
		if (login == null) { // 没有登录, 导航到登录界面
			//request.getRequestDispatcher("/login.html").forward(request,response);
			response.sendRedirect("/login.html");
			return false;
		} else {
			return true;
		}
	}

}
